package com.msw.mq.rest;

import com.msw.mq.consumer.StringConsumer;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author mashuangwei
 * @title: MQController
 * @projectName mq
 * @description: TODO
 * @date 2020-03-06
 */
@RestController
@RequestMapping("/mq")
public class MQController {
    @Autowired
    private DefaultMQProducer defaultMQProducer;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/query")
    public Object query() throws Exception {
        Message message = new Message();
        message.setBody("hello boy".getBytes("utf-8"));
        message.setTopic("mymq");
        message.setTags("tag1");
        return defaultMQProducer.send(message);
    }

    @GetMapping("/convertAndSend")
    public void queryRocketMQTemplate() throws Exception {
        rocketMQTemplate.convertAndSend("topicMQ" + ":tag1", "hello boy...".getBytes("utf-8"));
    }

    @GetMapping("/sendAndReceive")
    public Object sendAndReceive() {
        String replyString = rocketMQTemplate.sendAndReceive("stringRequestTopic:tagA", "request string", String.class);

        return replyString;
    }
}
